<?php
  // no direct access
  defined( '_JEXEC' ) or die( 'Restricted access' );
  jimport('joomla.application.component.controller');
  
  class ProfileController extends JController
  {  
    function display(){
    
      $document =& JFactory::getDocument();
      $view = & $this->getView($this->_name, $document->getType());
      
      $mposition = $this->getModel('position', 'positionmodel');
      $mteam     = $this->getModel('team', 'teammodel');
      $view->assign("positions", $mposition->get());
      $view->assign("teams", $mteam->get());
      
      $user = JFactory::getUser();
      $view->assign("user", $user);
      
      $mperson   = $this->getModel('person', 'personmodel');
      $mpic      = $this->getModel('pic', 'picmodel');
      $mplayer   = $this->getModel('player', 'playermodel');
      
      $person  = $mperson->get_person_by_user_id($user->id);
      if($person){
        $view->assign("person", $person);
        
        $pic    = $mpic->get($person->pic_id);
        $player = $mplayer->get_by_person_id($person->id);
        if($pic)
          $view->assign("pic", $pic);
        if($player)
          $view->assign("player", $player);
      }
      
      $view->display();
    }
    
    function profile_save(){
      
      $user = JFactory::getUser();
      if($user){
        $post = JRequest::get('post');
        $post['user_id'] = $user->id;
        
        $pic     = $this->save_pic($post);
        $post['pic_id'] = $pic->id;
        
        $person  = $this->save_person($post);
        $post['person_id'] = $person->id;
        
        $player  = $this->save_player($post);
        $post['player_id'] = $player->id;
      
        if($post['player_id'])
          $pic     = $this->save_pic($post);
        
        $user->name = $person->lastname.', '.$person->firstname;
        $user->save(true);
      }
      
      $this->setRedirect(JRoute::_('index.php?option=com_pbny&view=profile'), "Profile Saved!");
    }
          
    private function save_player($data){
    
      $mplayer = $this->getModel('player', 'playermodel');
      
      $player  = array();
      $player['person_id']     = $data['person_id'];
      $player['team_id']       = $data['team_id'];
      $player['jersey_number'] = $data['jersey_number'];
      $player['position_id']   = $data['position_id'];
      $player['id']            = $data['player_id'];
      
      $result = $mplayer->save($player);
      if($result->id)
        return $mplayer->get($result->id);
      return null;
    }
    
    private function save_person($data){
      
      $mperson = $this->getModel('person', 'personmodel');
      
      $person = array();
      $person['firstname'] = $data['firstname'];
      $person['lastname']  = $data['lastname'];
      $person['pic_id']    = $data['pic_id'];
      $person['phone_number'] = $data['phone_number'];
      $person['weight']    = $data['weight'];
      $person['height']    = $data['height'];
      $person['user_id']   = $data['user_id'];
      $person['id']        = $data['person_id'];
      
      $result = $mperson->save($person);
      if($result->id)
        return $mperson->get($result->id);
      return null;
    }
    
    private function save_pic($data){
    
      $mpic = $this->getModel('pic', 'picmodel');
      
      $location = PBNYHelper::downloadImage($data['pic'], $data['pic_url']);
      
      $pic['description'] = basename($location);
      $pic['pic']         = $location;
      $pic['pic_type']    = 'PLAYERS';
      $pic['pic_id']      = $data['player_id'];
      $pic['pic_url']     = $data['pic_url'];
      $pic['id']          = $data['pic_id'];
      
      $result = $mpic->save($pic);
      if($result->id)
        return $mpic->get($result->id);
      return null;
    }
  }
?>
